package health_system.health_system.config;

import health_system.health_system.service.FileStorageService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class AppInitializer {

    private final FileStorageService fileStorageService;
    private final AppProperties appProperties;
    
    @PostConstruct
    public void init() {
        log.info("Initializing application...");
        
        // 确保文件上传目录存在
        File uploadDir = new File(appProperties.getUpload().getDir());
        if (!uploadDir.exists()) {
            log.info("Creating upload directory: {}", uploadDir.getAbsolutePath());
            fileStorageService.init();
        }
        
        // 初始化其他必要的目录
        initSubDirectories();
        
        log.info("Application initialized successfully");
    }
    
    private void initSubDirectories() {
        // 创建媒体子目录
        createSubDirectory("avatars");
        createSubDirectory("moments");
        createSubDirectory("health");
        createSubDirectory("chats");
    }
    
    private void createSubDirectory(String dirName) {
        File dir = new File(appProperties.getUpload().getDir(), dirName);
        if (!dir.exists()) {
            log.info("Creating sub-directory: {}", dir.getAbsolutePath());
            dir.mkdirs();
        }
    }
} 